package org.wiztools.thegreatwall;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author arun
 */
public class AddPostServlet extends HttpServlet {

    private static Logger LOG = Logger.getLogger(AddPostServlet.class.getName());

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        LOG.info("Post Message Servlet: doPost() ");
        try {
            String postMsg = req.getParameter("postMsg");
            String wallIdStr = req.getParameter("wallId");
            LOG.info("message to be posted is " + postMsg);
            LOG.info("wall id is " + wallIdStr);
            Post p = new Post();
            //user id should be taken from session:
            p.setUserId(2);
            p.setWallId(Integer.parseInt(wallIdStr));
            p.setMessage(postMsg);
            new MySQLOperations().addPost(p);
            doGet(req, res);
        } catch (OperationException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        LOG.info("dispatching to ViewWallServlet");
        String wallName = req.getParameter("wallName");
        LOG.info("show the posts for the wall: "+wallName);
        req.getRequestDispatcher("/wall?"+"w="+wallName).forward(req, res);
    }

    
}
